<?php

namespace app\service\blog;

use app\model\blog\Article;
use app\model\Common;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class IndexService
{

    /**
     * 列表
     * @param int $page
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getList(int $page = 1): array
    {
        $page = abs($page);
        $list = Common::getList(ArticleTable, ['status' => 1], ['id', 'title', 'keywords', 'describe', 'create_time'], $page);
        if ($list) {
            foreach ($list['data'] as &$v) {
                $v['keywords'] = jsonToArray($v['keywords']);
                $v['create_time'] = date('Y-m-d');
            }
            return $list;
        }
        return [];
    }

    /**
     * 分类数据
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function categories(): array
    {
        $res = Article::getArticleClassifyData();
        if (!$res) {
            return [];
        }
        return $res;
    }

    /**
     * 详情
     * @param int $id
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function info(int $id): array
    {
        $res = Common::getData(ArticleTable, ['id' => $id], ['title', 'keywords', 'read_count', 'content_html', 'create_time']);
        if ($res) {
            $res['keywords'] = jsonToArray($res['keywords']);
            $res['create_time'] = date('Y-m-d H:i:s', $res['create_time']);
            $res['prev'] = Common::getData(ArticleTable, ['id' => $id - 1, 'status' => 1], ['id', 'title']);
            $res['next'] = Common::getData(ArticleTable, ['id' => $id + 1, 'status' => 1], ['id', 'title']);
            return $res;
        }
        return [];
    }

    /**
     * 归档数据
     * @param array $request
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function archives(array $request): array
    {
        $page = $request['page'] ?? 1;
        $field = ['id', 'title', 'create_time'];
        $res = Common::getList(ArticleTable, ['status' => 1], $field, $page);
        if ($res['count'] > 0) {
            $res['data'] = self::ArticleGroupByYear($res['data']);
        }
        return $res;
    }

    /**
     * 标签
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function tags(): array
    {
        $res = Common::getAllData(ArticleTable, ['status' => 1], ['keywords']);
        $list = [];
        if ($res['count'] >= 1) {
            foreach ($res['data'] as $v) {
                $v = jsonToArray($v['keywords']);
                foreach ($v as $val) {
                    if (!isset($list[$val])) {
                        $list[$val] = 1;
                    } else {
                        ++$list[$val];
                    }
                }
            }
        }
        return $list;
    }


    /**
     * 文章按年份分组
     * @param array $data
     * @return array
     */
    private static function ArticleGroupByYear(array $data): array
    {
        $list = [];
        foreach ($data as $v) {
            $year = date('Y', $v['create_time']);
            if (!isset($list[$year])) {
                $list[$year] = [];
            }
            $v['time'] = date('m-d', $v['create_time']);
            $list[$year][] = $v;
        }
        return $list;
    }
}
